﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKArchMethodLibrary.Utils;

namespace DormantWindow
{
	public partial class SelectComponentsTypeForm : System.Windows.Forms.Form
	{
		public int WallTypeIndex
		{
			get
			{
				return this.cmbWallType.SelectedIndex;
			}
		}

		public int RoofTypeIndex
		{
			get
			{
				return this.cmbRoofType.SelectedIndex;
			}
		}

		public int WindowTypeIndex
		{
			get
			{
				return this.cmbWindowType.SelectedIndex;
			}
		}

		public bool InfoChanged
		{
			get
			{
				return this.m_InfoChanged;
			}
			set
			{
				this.m_InfoChanged = value;
			}
		}

		public double DormantWidth
		{
			get
			{
				return Convert.ToDouble(this.tbDormantWidth.Text);
			}
		}

		public double MaxRidgeHeight
		{
			get
			{
				return Convert.ToDouble(this.tbMaxRidgeHeight.Text);
			}
		}

		public double WallHeight
		{
			get
			{
				return Convert.ToDouble(this.tbWallHeight.Text);
			}
		}

		public double EavesOffset
		{
			get
			{
				return Convert.ToDouble(this.tbEavesOffset.Text);
			}
		}

		public double WindowBottomElevation
		{
			get
			{
				return Convert.ToDouble(this.tbWindowElevation.Text);
			}
		}

		public SelectComponentsTypeForm(WindowGeometryExtractor extractor, List<WallType> wallTypeList, List<RoofType> roofTypeList, List<FamilySymbol> windowTypeList, double slopeAngle)
		{
			this.InitializeComponent();
			this.m_Extractor = extractor;
			this.m_SlopeAngle = slopeAngle;
			this.m_WallTypeList = wallTypeList;
			foreach (WallType wallType in wallTypeList)
			{
				this.cmbWallType.Items.Add(wallType.Name);
			}
			if (this.cmbWallType.Items.Count > 0)
			{
				this.cmbWallType.SelectedIndex = 0;
			}
			this.m_RoofTypeList = roofTypeList;
			foreach (RoofType roofType in roofTypeList)
			{
				this.cmbRoofType.Items.Add(roofType.Name);
			}
			if (this.cmbRoofType.Items.Count > 0)
			{
				this.cmbRoofType.SelectedIndex = 0;
			}
			this.m_WindowTypeList = windowTypeList;
			foreach (FamilySymbol familySymbol in windowTypeList)
			{
				this.cmbWindowType.Items.Add(familySymbol.Family.Name + "_" + familySymbol.Name);
			}
			if (this.cmbWindowType.Items.Count > 0)
			{
				this.cmbWindowType.SelectedIndex = 0;
			}
			int num = this.tbDormantWidth.Height / 5;
			this.m_Previewer = new Previewer(this, num, this.cmbWallType.Top, this.label7.Left - num * 2, this.tbWindowElevation.Bottom - this.cmbWallType.Top, 1u);
			base.MouseWheel += this.OnMouseWheel;
			this.m_Previewer.PreviewerControl().MouseDoubleClick += this.OnMouseDoubleClick;
			this.InitTransform();
		}

		private void InitTransform()
		{
			Transform transform = RevitVersionFuncs.CreateRotationAtPoint(XYZ.BasisX, -1.2566370614359172, XYZ.Zero);
			Transform transform2 = RevitVersionFuncs.CreateRotationAtPoint(XYZ.BasisY, -0.3490658503988659, XYZ.Zero);
			Transform transform3 = RevitVersionFuncs.CreateRotationAtPoint(XYZ.BasisZ, -0.11635528346628864, XYZ.Zero);
			this.m_Transform = transform3.Multiply(transform2.Multiply(transform));
		}

		private void SelectComponentsTypeForm_Load(object sender, EventArgs e)
		{
			this.m_IsInitialized = false;
			this.ReadData();
			this.GetWindowInfos();
			this.m_IsInitialized = true;
		}

		private void SelectComponentsTypeForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		protected void OnMouseWheel(object sender, MouseEventArgs e)
		{
			if (this.m_Previewer.Focused)
			{
				double angle = (double)e.Delta / 180.0 * Math.PI / 20.0;
				Transform transform = RevitVersionFuncs.CreateRotationAtPoint(XYZ.BasisZ, angle, XYZ.Zero);
				this.m_Transform = this.m_Transform.Multiply(transform);
				this.Refresh();
			}
		}

		protected void OnMouseDoubleClick(object sender, MouseEventArgs e)
		{
			this.InitTransform();
			this.Refresh();
			this.m_Previewer.ZoomE();
		}

		private void Redraw()
		{
			this.m_Previewer.Transparent(this.chbTransparent.Checked);
			this.m_Plotter = new DormantPlotter();
			FamilySymbol familySymbol = this.m_WindowTypeList[this.WindowTypeIndex];
			double num = familySymbol.get_Parameter(BuiltInParameter.FAMILY_HEIGHT_PARAM).AsDouble();
			num = Common.FeetToMM(num);
			double num2 = familySymbol.get_Parameter(BuiltInParameter.CASEWORK_WIDTH).AsDouble();
			num2 = Common.FeetToMM(num2);
			if (Geometry.IsEqual(num, 0.0))
			{
				num = num2;
			}
			double num3 = familySymbol.GetParameter("默认窗台高度").AsDouble();
			num3 = Common.FeetToMM(num3);
			double num4 = this.m_RoofTypeList[this.RoofTypeIndex].get_Parameter(BuiltInParameter.ROOF_ATTR_DEFAULT_THICKNESS_PARAM).AsDouble();
			num4 = Common.FeetToMM(num4);
			double num5 = this.m_WallTypeList[this.WallTypeIndex].get_Parameter(BuiltInParameter.WALL_ATTR_WIDTH_PARAM).AsDouble();
			num5 = Common.FeetToMM(num5);
			try
			{
				this.m_Plotter.Draw(this.m_Transform, this.DormantWidth, this.MaxRidgeHeight - this.WallHeight, this.WallHeight, num4, num5, num2, num, num3, this.WindowBottomElevation, this.m_WindowProfileLoop, this.m_SlopeAngle, this.EavesOffset, this.m_WindowFaces, this.m_WindowFacesIsGlass, this.chbTransparent.Checked, this.chbOnlyShowWindow.Checked);
			}
			catch (Exception)
			{
			}
		}

		private void GetWindowInfos()
		{
			this.m_WindowFaces.Clear();
			this.m_WindowFacesIsGlass.Clear();
			this.m_WindowProfileLoop.Clear();
			FamilySymbol symbol = this.m_WindowTypeList[this.WindowTypeIndex];
			this.m_Extractor.ExtractWindowGeometryInfo(symbol, ref this.m_WindowFaces, ref this.m_WindowFacesIsGlass, ref this.m_WindowProfileLoop);
		}

		protected override void DefWndProc(ref Message m)
		{
			switch (m.Msg)
			{
			case 1125:
				if (m.WParam.ToInt32() == 1 && this.m_IsInitialized)
				{
					this.Redraw();
					this.m_Previewer.Curves = this.m_Plotter.Curves;
					this.m_Previewer.TextNotes = this.m_Plotter.Texts;
					this.m_Previewer.FillRegions = this.m_Plotter.FillRegions;
					return;
				}
				break;
			case 1126:
				this.lbTip.Visible = true;
				return;
			case 1127:
				this.lbTip.Visible = false;
				return;
			default:
				base.DefWndProc(ref m);
				break;
			}
		}

		private void tbDormantWidth_TextChanged(object sender, EventArgs e)
		{
			if (!Common.IsValidating(this.tbDormantWidth.Text, 1.0, 5000.0))
			{
				MessageBox.Show("合法范围在1000mm～5000mm之间", "盈建科提示");
				this.tbDormantWidth.Text = this.m_DormantWidth;
				return;
			}
			if (Geometry.Greaterthan_Or_Equal(Convert.ToDouble(this.tbDormantWidth.Text), 1000.0))
			{
				this.m_DormantWidth = this.tbDormantWidth.Text;
			}
			this.Refresh();
			if (this.m_IsInitialized)
			{
				this.m_InfoChanged = true;
			}
		}

		private void tbMaxRidgeHeight_TextChanged(object sender, EventArgs e)
		{
			if (!Common.IsValidating(this.tbMaxRidgeHeight.Text, 1.0, 5000.0))
			{
				MessageBox.Show("合法范围在1000mm～5000mm之间", "盈建科提示");
				this.tbMaxRidgeHeight.Text = this.m_MaxRidgeHeight;
				return;
			}
			if (Geometry.Greaterthan_Or_Equal(Convert.ToDouble(this.tbMaxRidgeHeight.Text), 1000.0))
			{
				this.m_MaxRidgeHeight = this.tbMaxRidgeHeight.Text;
			}
			this.Refresh();
			if (this.m_IsInitialized)
			{
				this.m_InfoChanged = true;
			}
		}

		private void tbWallHeight_TextChanged(object sender, EventArgs e)
		{
			if (!Common.IsValidating(this.tbWallHeight.Text, 1.0, 4500.0))
			{
				MessageBox.Show("合法范围在500mm～4500mm之间", "盈建科提示");
				this.tbWallHeight.Text = this.m_WallHeight;
				return;
			}
			if (Geometry.Greaterthan_Or_Equal(Convert.ToDouble(this.tbWallHeight.Text), 500.0))
			{
				this.m_WallHeight = this.tbWallHeight.Text;
			}
			this.Refresh();
			if (this.m_IsInitialized)
			{
				this.m_InfoChanged = true;
			}
		}

		private void tbEavesOffset_TextChanged(object sender, EventArgs e)
		{
			if (!Common.IsValidating(this.tbEavesOffset.Text, 0.0, 500.0))
			{
				MessageBox.Show("合法范围在0mm～500mm之间", "盈建科提示");
				this.tbEavesOffset.Text = this.m_EavesOffset;
				return;
			}
			this.m_EavesOffset = this.tbEavesOffset.Text;
			this.Refresh();
		}

		private void tbWindowElevation_TextChanged(object sender, EventArgs e)
		{
			if (!Common.IsValidating(this.tbWindowElevation.Text, 0.0, 1500.0))
			{
				MessageBox.Show("合法范围在0mm～1500mm之间", "盈建科提示");
				this.tbWindowElevation.Text = this.m_WindowBottomElevation;
				return;
			}
			this.m_WindowBottomElevation = this.tbWindowElevation.Text;
			this.Refresh();
			this.GetWindowInfos();
		}

		private void chbTransparent_CheckedChanged(object sender, EventArgs e)
		{
			this.Refresh();
		}

		private void cmbWallType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.Refresh();
		}

		private void cmbRoofType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.Refresh();
		}

		private void cmbWindowType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.GetWindowInfos();
			this.Refresh();
		}

		private void chbOnlyShowWindow_CheckedChanged(object sender, EventArgs e)
		{
			this.Refresh();
		}

		private void ReadData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "SelectComponentsTypeForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.m_DormantWidth = "4000";
				this.m_MaxRidgeHeight = "3500";
				this.m_WallHeight = "2200";
				this.m_EavesOffset = "500";
				this.m_WindowBottomElevation = "600";
				this.tbDormantWidth.Text = this.m_DormantWidth;
				this.tbMaxRidgeHeight.Text = this.m_MaxRidgeHeight;
				this.tbWallHeight.Text = this.m_WallHeight;
				this.tbEavesOffset.Text = this.m_EavesOffset;
				this.tbWindowElevation.Text = this.m_WindowBottomElevation;
				this.chbTransparent.Checked = false;
				this.chbOnlyShowWindow.Checked = false;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			this.m_DormantWidth = streamReader.ReadLine();
			this.tbDormantWidth.Text = this.m_DormantWidth;
			this.m_MaxRidgeHeight = streamReader.ReadLine();
			this.tbMaxRidgeHeight.Text = this.m_MaxRidgeHeight;
			this.m_WallHeight = streamReader.ReadLine();
			this.tbWallHeight.Text = this.m_WallHeight;
			this.m_EavesOffset = streamReader.ReadLine();
			this.tbEavesOffset.Text = this.m_EavesOffset;
			this.m_WindowBottomElevation = streamReader.ReadLine();
			this.tbWindowElevation.Text = this.m_WindowBottomElevation;
			this.chbTransparent.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.chbOnlyShowWindow.Checked = Convert.ToBoolean(streamReader.ReadLine());
			int num = Convert.ToInt32(streamReader.ReadLine());
			if (num < this.cmbWallType.Items.Count)
			{
				this.cmbWallType.SelectedIndex = num;
			}
			int num2 = Convert.ToInt32(streamReader.ReadLine());
			if (num2 < this.cmbRoofType.Items.Count)
			{
				this.cmbRoofType.SelectedIndex = num2;
			}
			int num3 = Convert.ToInt32(streamReader.ReadLine());
			if (num3 < this.cmbWindowType.Items.Count)
			{
				this.cmbWindowType.SelectedIndex = num3;
			}
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "SelectComponentsTypeForm.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.m_DormantWidth);
			streamWriter.WriteLine(this.m_MaxRidgeHeight);
			streamWriter.WriteLine(this.m_WallHeight);
			streamWriter.WriteLine(this.m_EavesOffset);
			streamWriter.WriteLine(this.m_WindowBottomElevation);
			streamWriter.WriteLine(this.chbTransparent.Checked);
			streamWriter.WriteLine(this.chbOnlyShowWindow.Checked);
			streamWriter.WriteLine(this.cmbWallType.SelectedIndex);
			streamWriter.WriteLine(this.cmbRoofType.SelectedIndex);
			streamWriter.WriteLine(this.cmbWindowType.SelectedIndex);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void PressESC()
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			SelectComponentsTypeForm.keybd_event(bVk, 0, dwFlags, 0);
			SelectComponentsTypeForm.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void SelectComponentsTypeForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.PressESC();
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SendSetFocusMessage()
		{
			int wMsg = 7;
			SelectComponentsTypeForm.SendMessage(DormantWindowCreator._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private void SelectComponentsTypeForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				if (!Common.IsValidating(this.tbDormantWidth.Text, 1000.0, 5000.0))
				{
					MessageBox.Show("合法范围在1000mm～5000mm之间", "盈建科提示");
					this.tbDormantWidth.Text = this.m_DormantWidth;
					return;
				}
				if (!Common.IsValidating(this.tbMaxRidgeHeight.Text, 1000.0, 5000.0))
				{
					MessageBox.Show("合法范围在1000mm～5000mm之间", "盈建科提示");
					this.tbMaxRidgeHeight.Text = this.m_MaxRidgeHeight;
					return;
				}
				if (!Common.IsValidating(this.tbWallHeight.Text, 500.0, 4500.0))
				{
					MessageBox.Show("合法范围在500mm～4500mm之间", "盈建科提示");
					this.tbWallHeight.Text = this.m_WallHeight;
					return;
				}
				this.SendSetFocusMessage();
				if (this.m_InfoChanged)
				{
					this.PressESC();
					return;
				}
			}
			else
			{
				base.Focus();
			}
		}

		private void SelectComponentsTypeForm_Activated(object sender, EventArgs e)
		{
			if (Cursor.Position.X < base.Left || Cursor.Position.X > base.Right || Cursor.Position.Y < base.Top || Cursor.Position.Y > base.Bottom)
			{
				Cursor.Position = new System.Drawing.Point((int)((double)base.Left + (double)(base.Right - base.Left) / 2.0), (int)((double)base.Top + (double)(base.Bottom - base.Top) / 2.0));
			}
		}

		private WindowGeometryExtractor m_Extractor;

		private Previewer m_Previewer;

		private Transform m_Transform;

		private DormantPlotter m_Plotter;

		private List<WallType> m_WallTypeList;

		private List<RoofType> m_RoofTypeList;

		private List<FamilySymbol> m_WindowTypeList;

		private double m_SlopeAngle;

		private string m_DormantWidth = "";

		private string m_MaxRidgeHeight = "";

		private string m_WallHeight = "";

		private string m_EavesOffset = "";

		private string m_WindowBottomElevation = "";

		private bool m_IsInitialized;

		private bool m_InfoChanged;

		private List<List<EdgeInfo>> m_WindowFaces = new List<List<EdgeInfo>>();

		private List<bool> m_WindowFacesIsGlass = new List<bool>();

		private List<XYZ> m_WindowProfileLoop = new List<XYZ>();
	}
}
